/*
 * Filename:    KpiDataItemSummaryServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月11日
 */
package com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.quota.calculationreport.calculationdataItem.biz.service.SummaryService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationdataItem.dto.CalculationResultDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.IKpiDataItemSummaryService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.KpiJobCtrlService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.constants.CalculationJobConstants;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.HcpmsKpiJobCtrlDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.HcpmsKpiJobParamDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchDetailDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchJobLogDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchParamterDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.intergration.dao.KpiBatchDao;
import com.pingan.um.client.util.UUID;

/**
 * 数据汇总job实现类
 * 
 * @author DUQIFANG262
 * 
 */
@Component("kpiDataItemSummaryServiceImpl")
public class KpiDataItemSummaryServiceImpl implements IKpiDataItemSummaryService {

	public static Log log = LogFactory.getLog(KpiDataItemSummaryServiceImpl.class);

	public static final String userName = "ADMIN";

	@Autowired
	private KpiBatchDao kpiBatchDao;
	@Autowired
	private SummaryService summaryService;
	@Autowired
	private KpiJobCtrlService kpiJobCtrlService;

	/**
	 * 
	 * 数据项汇总计算job方法
	 * 
	 */
	@Override
	public void processDataItemSummaryCalculation() {

		log.info("-----数据项汇总job进入-----");
		HcpmsKpiJobCtrlDTO ctrlDto = new HcpmsKpiJobCtrlDTO();
		ctrlDto.setJobCode("J20141217002");
		ctrlDto.setJobName("数据项汇总JOB");
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		ctrlDto.setTmpId(UUID.getID());
		ctrlDto = kpiJobCtrlService.getHcpmsJobCtrl(ctrlDto);
		// if (ctrlDto != null) {
		//
		// ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		// kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);
		// }
		if (ctrlDto == null) {
			log.info("-----没有数据进行数据项汇总或者JOB被占用-----");
			return;
		}
		// String uuid = UUID.getID();
		//
		// this.kpiBatchDao.updateKpiBatchDetailDTOTempBatchByStatus(uuid,
		// CalculationJobConstants.KPI_BATCH_STATUS_00);

		// 根据当前的临时主建和状态是待处理的00 加载出来
		KpiBatchParamterDTO paramter = new KpiBatchParamterDTO();
		paramter.setStatus(CalculationJobConstants.KPI_BATCH_STATUS_00);
		// paramter.setTempBatch(uuid);

		List<KpiBatchDetailDTO> list = this.kpiBatchDao.queryKpiBatchDetailDTO(paramter);

		if (null != list && list.size() > 0) {

			for (KpiBatchDetailDTO bean : list) {
				String id = bean.getIdKpiBatchDetail();
				String regionCode = bean.getRegionCode();
				String batchNo = bean.getBatchNo();
				String month = bean.getMonth();
				// 更新当前指标批次明细表的状态为数据项汇总中
				this.kpiBatchDao.updateKpiBatchDetailDTOStatus(id, CalculationJobConstants.KPI_BATCH_DETAIL_01);

				// 数据项汇总开始

				log.info("-------汇总开始------");

				// // 数据项汇总就诊信息-日
				// CalculationResultDTO visterResultDay =
				// this.summaryService.saveSummaryVisitDay(userName, batchNo);
				// 数据项汇总就诊信息-月
				KpiBatchJobLogDTO logDto = new KpiBatchJobLogDTO();
				logDto.setBatchNo(batchNo);
				logDto.setCreateBy(bean.getCreatedBy());
				logDto.setUpdateBy(bean.getUpdatedBy());
				logDto.setCreateDate(new Date());
				logDto.setJobName(ctrlDto.getJobName());
				logDto.setJobNameDesc(KpiDataItemServiceImpl.class.getName());
				HcpmsKpiJobParamDTO dto = new HcpmsKpiJobParamDTO();
				dto.setBatchNo(batchNo);
				dto.setPeriod(CalculationJobConstants.KPI_PERIOD_MONTH);
				dto.setRegionCode(regionCode);
				dto.setMonth(month);
				dto.setUserId(bean.getCreatedBy());
				CalculationResultDTO visterResultMonth = this.summaryService.saveSummaryVisitMonth(dto);
				logDto.setUpdateDate(new Date());
				logDto.setOpertionTime(new Date());
				logDto.setSuccessMessage(visterResultMonth.getMessage(), regionCode, month);
				if (CalculationJobConstants.status_N.equals(visterResultMonth.getIsError())) {
					logDto.setStatus(CalculationJobConstants.status_01);
				} else {
					logDto.setStatus(CalculationJobConstants.status_02);
					return;
				}
				kpiJobCtrlService.insertKpiJobLog(logDto);
				// // 数据项汇总单据信息-日
				// CalculationResultDTO sigleResultDay =
				// this.summaryService.saveSummaryBillDay(userName, batchNo);

				// 数据项汇总单据信息-月
				logDto.setCreateDate(new Date());
				CalculationResultDTO sigleResultMonth = this.summaryService.saveSummaryBillMonth(dto);
				logDto.setUpdateDate(new Date());
				logDto.setOpertionTime(new Date());
				logDto.setSuccessMessage(sigleResultMonth.getMessage(), regionCode, month);
				if (CalculationJobConstants.status_N.equals(sigleResultMonth.getIsError())) {
					logDto.setStatus(CalculationJobConstants.status_01);
				} else {
					logDto.setStatus(CalculationJobConstants.status_02);
					return;
				}
				kpiJobCtrlService.insertKpiJobLog(logDto);
				if (
				// CalculationJobConstants.status_N.equals(visterResultDay.getIsError())&&
				CalculationJobConstants.status_N.equals(visterResultMonth.getIsError()) &&
				// CalculationJobConstants.status_N.equals(sigleResultDay.getIsError())&&
						CalculationJobConstants.status_N.equals(sigleResultMonth.getIsError())) {

					// 更新当前指标批次明细表的状态为数据项汇总完成
					this.kpiBatchDao.updateBatchDetailTempBatchIsNull(CalculationJobConstants.KPI_BATCH_DETAIL_02, id);

					// // 保存日志
					// this.saveSummaryBatchLog(visterResultDay,
					// visterResultMonth, sigleResultDay, sigleResultMonth,
					// batchNo);

				} else {

					// 更新当前指标批次明细表的状态为数据项汇总失败
					this.kpiBatchDao.updateKpiBatchDetailDTOStatus(id, CalculationJobConstants.KPI_BATCH_DETAIL_20);

					// // 保存日志
					// this.saveSummaryBatchLog(visterResultDay,
					// visterResultMonth, sigleResultDay, sigleResultMonth,
					// batchNo);

				}
				log.info("-------汇总结束------");
			}
		} else {
			log.info("-----没有要汇总的数据-----");
		}
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_IDLE);
		ctrlDto.setTmpId(null);
		kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);

	}

	public void saveSummaryBatchLog(CalculationResultDTO visterResultDay, CalculationResultDTO visterResultMonth,
			CalculationResultDTO sigleResultDay, CalculationResultDTO sigleResultMonth, String batchNo) {

		if (CalculationJobConstants.status_N.equals(visterResultDay.getIsError())) { // 数据项汇总就诊信息-日成功

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_01, visterResultDay.getMessage(), batchNo);

		} else if (CalculationJobConstants.status_Y.equals(visterResultDay.getIsError())) {// //
																							// 数据项汇总就诊信息-日
																							// 失败
			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_02, visterResultDay.getMessage(), batchNo);

		}

		if (CalculationJobConstants.status_N.equals(visterResultMonth.getIsError())) {// 数据项汇总就诊信息-月
																						// 成功

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_01, visterResultMonth.getMessage(), batchNo);

		} else if (CalculationJobConstants.status_Y.equals(visterResultMonth.getIsError())) {// 数据项汇总就诊信息-月
																								// 失败

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_02, visterResultMonth.getMessage(), batchNo);

		}

		if (CalculationJobConstants.status_N.equals(sigleResultDay.getIsError())) { // 数据项汇总单据信息-日
																					// 成功

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_01, sigleResultDay.getMessage(), batchNo);

		} else if (CalculationJobConstants.status_Y.equals(sigleResultDay.getIsError())) { // 数据项汇总单据信息-日
																							// 失败

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_02, sigleResultDay.getMessage(), batchNo);

		}

		if (CalculationJobConstants.status_N.equals(sigleResultMonth.getIsError())) { // 数据项汇总单据信息-月
																						// 成功\

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_01, sigleResultMonth.getMessage(), batchNo);

		} else if (CalculationJobConstants.status_Y.equals(sigleResultMonth.getIsError())) {// 数据项汇总单据信息-月
																							// 失败

			this.saveKpiBatchJobLogDTOMethod(CalculationJobConstants.status_02, sigleResultMonth.getMessage(), batchNo);

		}

	}

	private void saveKpiBatchJobLogDTOMethod(String status, String exceptionMessage, String batchNo) {
		KpiBatchJobLogDTO bean = new KpiBatchJobLogDTO();
		bean.setBatchNo(batchNo);
		bean.setCreateBy("ADMIN");
		bean.setUpdateBy("ADMIN");
		bean.setJobExceptionMessage(exceptionMessage);
		bean.setJobName(KpiDataItemSummaryServiceImpl.class.getName());
		bean.setJobNameDesc("数据项汇总计算job");
		bean.setOpertionTime(new Date());
		bean.setStatus(status);
		this.kpiBatchDao.insertBacthJobLogDTO(bean);
	}
}
